package threadtask;
import com.alibaba.fastjson.JSONObject;
import constants.ConfigConstant;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import utils.HttpUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author yuyufeng
 * @date 2018/2/7
 */
public class ScheduledTask implements Runnable {
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());
    @Override
    public void run() {
        System.out.println("MPTest.test");
        String host = "https://api.weixin.qq.com";
        String path = "/cgi-bin/token";
        Map<String, String> headers = new HashMap<>();
        Map<String, String> querys = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        querys.put("grant_type", "client_credential");
        querys.put("appid", "wxa3f5269ed575a816");
        querys.put("secret", "6cd0d8bec09f4d450acd07abaabb356d");
        HttpResponse response = null;
        try {
            response = HttpUtils.doPost(host,path,"GET",headers,querys,bodys);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String result = null;
        JSONObject jsonObject = null;
        try {
            result = EntityUtils.toString(response.getEntity());
            jsonObject = JSONObject.parseObject(result);
            ConfigConstant.mpAccessToken = jsonObject.getString("access_token");
        } catch (IOException e) {
            e.printStackTrace();
        }
        LOG.info(result);


    }

    public static void main(String[] args) {
        ExecutorService es = Executors.newSingleThreadExecutor();
        es.execute(new ScheduledTask());
    }
}
